package com.app.micaihu.view.main.game;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.app.micaihu.R;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.base.BaseOldActivity;
import com.app.micaihu.base.bean.DataBean;
import com.app.micaihu.bean.comment.NewsComment;
import com.app.micaihu.bean.game.GameRaidersContent;
import com.app.micaihu.configure.Config;
import com.app.micaihu.configure.ConfigKeyConstant;
import com.app.micaihu.configure.TypeConstant;
import com.app.micaihu.configure.URIContant;
import com.app.micaihu.configure.UserConfigTools;
import com.app.micaihu.custom.components.downandupload.DownloadUtils;
import com.app.micaihu.custom.view.CustomImageView;
import com.app.widget.LoadView;
import com.app.micaihu.custom.view.SlideSelectView;
import com.app.micaihu.custom.view.dialog.ReportDialog;
import com.app.micaihu.custom.view.listivew.CommentListView;
import com.app.micaihu.listener.CommentListener;
import com.app.micaihu.listener.HttpListener;
import com.app.micaihu.manager.AppConfigManager;
import com.app.micaihu.manager.UserInforManager;
import com.app.micaihu.utils.AppShareUtils;
import com.app.micaihu.utils.CollectUtils;
import com.app.micaihu.utils.CommentUtils;
import com.app.micaihu.videoplayer.JCVideoPlayer;
import com.app.micaihu.view.main.MainActivity;
import com.app.micaihu.view.main.game.view.GameDetailVideoView;
import com.app.micaihu.view.newsdetail.ImageShowActivity;
import com.app.micaihu.view.newsdetail.view.DetailScrollView;
import com.app.micaihu.view.user.userinfo.activity.WebActivity;
import com.app.utils.util.AppTools;
import com.app.utils.util.StringUtils;
import com.app.utils.util.ToastUtils;
import com.app.utils.util.ViewSizeUtils;
import com.app.utils.util.image.LoadImageUtils;
import com.app.utils.util.osutils.StatusBarUtils;
import com.baidu.mobstat.StatService;
import com.blankj.utilcode.util.ActivityUtils;
import com.google.gson.reflect.TypeToken;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;
import com.umeng.socialize.UMShareAPI;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 游戏攻略视频详情页
 * Created by linkai on 2017/3/17.
 */
@SuppressLint("JavascriptInterface")
public class GameRaidersVideoActivity extends BaseOldActivity implements View.OnClickListener, GameDetailVideoView.OnViewScrollListener {

    /*********
     * 页面控件变量
     *********/
    private DetailScrollView mScrollView;//滑动布局
    private CommentListView mCommentListView;//评论载体
    private LoadView mNewsLoadView;//文章加载提示
    private View mHeadAuthorView;//頂部作者模块
    private TextView mCommentSum;//评论总数
    private TextView mShareSum;//分享总数
    private ImageView mCollectView;//收藏
    private GameDetailVideoView detailNewsView;//内容view


    /*********
     * 页面逻辑数据
     *********/
    private GameRaidersContent mNewsContent;//页面内容
    private String newsId;//文章id
    private String gameid;//游戏id
    private boolean onPageFinish;//是否加载成功
    private boolean isTinyWindowPlayer;//当前是否为小窗

    /***********
     * 页面工具
     *************/
    private CommentListener mCommentListener;//评论监听
    private AppShareUtils mShareUtils;//分享工具
    private AppShareUtils mShareUtils1;//分享工具(右上角的分享工具)
    private ReportDialog reportDialog;
    private View driver;
    private ImageView backView;
    private ImageView moreView;
    private View titlelayout;
    private RelativeLayout stickHeader;
    private TextView headDesc;
    private CustomImageView headCiv;
    private TextView headTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isInit = false;
        getIntentData();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_raiders_video);
        StatusBarUtils.setupStatusBarView(this, R.color.common_bg_color_8, R.color.common_bg_color_8);
        StatusBarUtils.setStatusBarTextColor(this, true);
        initView();//初始化页面
        loadData();//初始化页面数据
        querySaveNews();//查询本地收藏
    }


    /**
     * 初始化控件
     */
    private void initView() {
//        findViewById(R.id.page_head_function).setOnClickListener(this);
        this.detailNewsView = (GameDetailVideoView) findViewById(R.id.dnv_newsview);
        this.detailNewsView.init();//控件内部初始化
        this.detailNewsView.httpName = mPageName;
        this.mScrollView = (DetailScrollView) findViewById(R.id.psv_psrsolview);
        this.mCommentListView = (CommentListView) findViewById(R.id.prlv_listview);
        this.mNewsLoadView = (LoadView) findViewById(R.id.lv_loadview);
        this.mNewsLoadView.setErrorPageClickListener(this);
        View commentView = findViewById(R.id.tv_comment);
        mCommentSum = (TextView) findViewById(R.id.tv_commentSum);
        mShareSum = (TextView) findViewById(R.id.tv_shareSum);
        View commentLayout = findViewById(R.id.newsDetail_comment_layout);
        ViewGroup.LayoutParams params = commentView.getLayoutParams();
        params.width = ViewSizeUtils.getWidthPixels() * 3 / 7;
        commentLayout.requestLayout();
        commentLayout.invalidate();
        View share = findViewById(R.id.iv_share);
        share.setOnClickListener(this);
        findViewById(R.id.iv_report).setOnClickListener(this);
        mCollectView = (ImageView) findViewById(R.id.iv_collect);
        mCollectView.setOnClickListener(this);
        commentView.setOnClickListener(this);
        commentLayout.setOnClickListener(this);

        //配置webview
        configWebView();


        //线
        driver = findViewById(R.id.game_divider);
        driver.setVisibility(View.GONE);

        //后退按钮
        backView = (ImageView) findViewById(R.id.game_left);
        backView.setImageResource(R.drawable.common_backbtn_icon);

        //右侧更多
        moreView = (ImageView) findViewById(R.id.game_right);
        moreView.setImageResource(R.drawable.detail_menu_white_selector);
        moreView.setOnClickListener(this);

        //背景
        titlelayout = findViewById(R.id.game_bg);
        titlelayout.setBackgroundColor(getResources().getColor(R.color.transparent));


        stickHeader = (RelativeLayout) findViewById(R.id.page_head_layout_false);
        stickHeader.setVisibility(View.GONE);


        //初始化假的头部
        mHeadAuthorView = findViewById(R.id.page_head_layout_false);
        headTitle = (TextView) mHeadAuthorView.findViewById(R.id.game_title);

        headCiv = (CustomImageView) mHeadAuthorView.findViewById(R.id.game_logo);

        headDesc = (TextView) mHeadAuthorView.findViewById(R.id.game_desc);

        TextView btn = (TextView) mHeadAuthorView.findViewById(R.id.game_download);
        btn.setOnClickListener(detailNewsView);


    }


    /* 初始化webview */
    @SuppressLint("SetJavaScriptEnabled")
    private void configWebView() {
        if (detailNewsView != null && detailNewsView.webview != null && !detailNewsView.webview.getIsDestory()) {
            detailNewsView.webview.setWebViewClient(new MyWebViewClient());
        }

    }

    /**
     * 获取传输的页面数
     */
    private void getIntentData() {
        Intent intent = getIntent();
        if (intent == null) {
            return;
        }
        newsId = intent.getStringExtra(Config.Extra.PARAMETER1);
        gameid = intent.getStringExtra(Config.Extra.PARAMETER2);

    }

    /************************ 操作数据的方法 start ******************/

    /**
     * 加载数据
     */
    private void loadData() {

        HashMap<String, String> parmsMap = new HashMap<String, String>();
        parmsMap.put("articleId", newsId + "");
        parmsMap.put("gameId", gameid + "");

        if (UserInforManager.getInstance().isLogin()) {
            parmsMap.put("uid", UserInforManager.getInstance().getUserInfor().getUid());
        }
        HttpListener<DataBean<GameRaidersContent>> httpListener = new HttpListener<DataBean<GameRaidersContent>>() {

            @Override
            public void onStart() {
                if (mNewsLoadView != null) {
                    mNewsLoadView.showLoadPage("");
                }
            }

            @Override
            public void onError(VolleyError error) {
                if (mNewsLoadView != null) {
                    mNewsLoadView.showErrorPage(AppApplication.getInstance().getString(R.string.neterror_click));
                }
            }

            @Override
            public void onSuccess(DataBean<GameRaidersContent> response) {
                if (response.noError() && !isFinishing()) {
                    mNewsContent = response.getData();
//                    mNewsContent.setGameId(gameid);

                    if (!TextUtils.isEmpty(mNewsContent.getGameId())) {
                        gameid = mNewsContent.getGameId();
                    }

                    //适配数据
                    adapterUI();

                } else {
                    mNewsLoadView.showErrorPage(response.getMsg());
                }
            }
        };
        //请求网络
        addPostCacheRequest(URIContant.GAME_RAIDERS_DETAIL, new TypeToken<DataBean<GameRaidersContent>>() {
        }.getType(), parmsMap, httpListener);
        // 添加js交互接口类，并起别名 imagelistner
    }

    /**
     * 适配数据
     */
    private void adapterUI() {
        if (mNewsContent == null || detailNewsView == null || detailNewsView.webview == null) {
            mNewsLoadView.showErrorPage(DataBean.getErrorMsg());
            return;
        }

        /************* 适配顶部内容页 ***********/
        // 添加js交互接口类，并起别名 imagelistner
        this.detailNewsView.webview.addJavascriptInterface(new JavascriptInterface(mActivity), "imagelistner");
        //加载新闻内容
        detailNewsView.setNewsData(mNewsContent);


        //刷新隐藏的头部
        headTitle.setText(mNewsContent.getAppName());
        LoadImageUtils.getInstance().loadImage(headCiv, mNewsContent.getAppIcon());
        String downStr = "<font color='#FF8200'>" + mNewsContent.getDownloadNum() + "</font>人下载";
        headDesc.setText(Html.fromHtml(downStr));


        //新闻文章逻辑
        /************* 适配底部评论页 ***********/
        if (!TextUtils.isEmpty(mNewsContent.getCommentNum()) && !"0".equals(mNewsContent.getCommentNum())) {
            this.mCommentSum.setText(mNewsContent.getCommentNum());
            this.mCommentSum.setVisibility(View.VISIBLE);
        }

        //设置分享数量
        if (!TextUtils.isEmpty(mNewsContent.getShareNum()) && !"0".equals(mNewsContent.getShareNum())) {
            this.mShareSum.setText(mNewsContent.getShareNum());
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) this.mShareSum.getLayoutParams();
            if (layoutParams != null) {
                layoutParams.width = RelativeLayout.LayoutParams.WRAP_CONTENT;
                layoutParams.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
                this.mShareSum.setLayoutParams(layoutParams);
            }
        }

        //如果当前有账户登陆则显示账户的收藏状态
        if (UserInforManager.getInstance().isLogin()) {
            //判断是否收藏
            if (mNewsContent.getIsCollect()) {
                this.mCollectView.setImageResource(R.drawable.infor_tabbar_collection_vis);
                this.mCollectView.setTag(true);
            } else {
                this.mCollectView.setImageResource(R.drawable.detail_collect_black_selector);
                this.mCollectView.setTag(false);
            }
        }
        //适配评论
        mCommentListView.setEmptyViewOnClickListener(GameRaidersVideoActivity.this);
        //加载评论
        mCommentListView.startLoadData(GameRaidersVideoActivity.this, mNewsContent.getArticleId(), mPageName);

    }


    // 注入js函数监听
    private void addImageClickListner() {
        // 这段js函数的功能就是，遍历所有的img几点，并添加onclick函数，在还是执行的时候调用本地接口传递url过去
        detailNewsView.webview.loadUrl("javascript:(function(){"
                + "var objs = document.getElementsByTagName(\"img\");"
                + "var imgurl=''; " + "for(var i=0;i<objs.length;i++)  " + "{"
                + "imgurl+=objs[i].getAttribute('data-original')+',';"
                + "objs[i].setAttribute('myid',''+i);"
                + "objs[i].onclick=function()  " + "    {  "
                + "var myid=this.getAttribute('myid');"
                + "  window.imagelistner.openImage(imgurl,myid);  " + "    }  "
                + "}" + "})()");
    }

    @Override
    public void onViewScrolling(int l, int t, int oldl, int oldt) {


        if (detailNewsView == null) return;

        if (t > detailNewsView.getVideoTinyTop() - ViewSizeUtils.dp2px(this, 44)) {
            if (stickHeader.getVisibility() == View.GONE) {
                stickHeader.setVisibility(View.VISIBLE);
            }

            if (driver.getVisibility() == View.GONE) {
                driver.setVisibility(View.VISIBLE);
                //后退按钮
                backView.setImageResource(R.drawable.common_back_selector);
                //右侧更多
                moreView.setImageResource(R.drawable.detail_menu_black_selector);
                //背景
                titlelayout.setBackgroundColor(getResources().getColor(R.color.common_bg_color_2));
            }

        } else if (t < detailNewsView.getVideoTinyTop() - ViewSizeUtils.dp2px(this, 44)) {
            if (stickHeader.getVisibility() == View.VISIBLE) {
                stickHeader.setVisibility(View.GONE);
            }
            if (driver.getVisibility() == View.VISIBLE) {
                driver.setVisibility(View.GONE);
                //后退按钮
                backView.setImageResource(R.drawable.common_backbtn_icon);
                //右侧更多
                moreView.setImageResource(R.drawable.detail_menu_white_selector);
                //背景
                titlelayout.setBackgroundColor(getResources().getColor(R.color.transparent));
            }

        }


        /**
         * 小窗视频
         */
        if (detailNewsView.jcVideoPlayerGame != null) {
            if (detailNewsView.jcVideoPlayerGame.isPlaying()) {

            }
            if (t > detailNewsView.getVideoTinyTop() && !isTinyWindowPlayer) {
                FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(400, 226);
                lp.gravity = Gravity.RIGHT | Gravity.BOTTOM;
                lp.setMargins(0, 0, 0, ViewSizeUtils.dp2px(this, 45));
                detailNewsView.jcVideoPlayerGame.startWindowTiny(lp);
                isTinyWindowPlayer = true;
            } else if (t < detailNewsView.getVideoTinyTop() && isTinyWindowPlayer) {
                new Handler().post(new Runnable() {
                    public void run() {
                        detailNewsView.jcVideoPlayerGame.backPress();
                        isTinyWindowPlayer = false;
                    }
                });

            }
        }
    }


    // js通信接口
    public class JavascriptInterface {

        private Context context;

        public JavascriptInterface(Context context) {
            this.context = context;
        }

        @android.webkit.JavascriptInterface
        public void openImage(String img, String index) {
            Intent intent = new Intent();
            String[] imgs = img.split(",");
            ArrayList<String> imgsUrl = new ArrayList<String>();
            for (String s : imgs) {
                imgsUrl.add(s);
            }
            intent.putStringArrayListExtra(Config.Extra.PARAMETER1, imgsUrl);
            intent.putExtra(Config.Extra.PARAMETER2, Integer.valueOf(index));
            if (mNewsContent != null) {
                intent.putExtra("title", mNewsContent.getShareTitle());
            }
            intent.setClass(context, ImageShowActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            overridePendingTransition(R.anim.base_alpha_in, R.anim.base_slide_remain);// 前一个参数是下一个Activity的效果
        }
    }


    // 监听
    private class MyWebViewClient extends WebViewClient {


        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (TextUtils.isEmpty(url) || url.length() < 5) {
                return false;
            }
            //判断是否为http协议，如果是自定义协议则交给系统
            String httpStr = "";
            try {
                httpStr = url.substring(0, 4);
            } catch (Exception e1) {
                e1.printStackTrace();
                return false;
            }
            if (!TextUtils.isEmpty(httpStr) && !httpStr.equals("http")
                    && !httpStr.equals("https") && !httpStr.equals("abou")) {
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                Uri content_url = Uri.parse(url);
                intent.setData(content_url);
                try {
                    startActivity(intent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return true;
            } else if (httpStr.equals("abou")) {
                return true;
            } else {
                //如果是正常稿子则跳转浏览器
                Intent intent = new Intent(GameRaidersVideoActivity.this, WebActivity.class);
                intent.putExtra("url", url);
                startActivity(intent);
                return true;
            }
        }


        @Override
        public void onPageFinished(WebView view, String url) {
            //因为 webview的onPageFinished 会执行两次
            // 所以这里逻辑处理一下 只执行一次
            if (!onPageFinish && !GameRaidersVideoActivity.this.isFinishing()) {
                onPageFinish = true;
                //隐藏等待提示
                mNewsLoadView.showSuccess();
                //添加JS函数
                addImageClickListner();
                //小视频的滑动监听
                detailNewsView.setOnViewScrollListener(GameRaidersVideoActivity.this);
                //加载相关新闻模块
                detailNewsView.setAboutData(gameid);
            }
        }

        @Override
        public void onReceivedError(WebView view, int errorCode,
                                    String description, String failingUrl) {
            // progressBar.setVisibility(View.GONE);
            view.goBack();
            super.onReceivedError(view, errorCode, description, failingUrl);
        }

    }


    /**
     * 查询当前文文章是否本地收藏
     */
    private void querySaveNews() {
        //如果当前用户已登录则不查询本地
        if (TextUtils.isEmpty(this.newsId) || UserInforManager.getInstance().isLogin()) {
            return;
        }
        //查询本地收藏
        boolean issave = CollectUtils.getInstance().querySaveId(this.newsId);
        if (issave) {
            this.mCollectView.setImageResource(R.drawable.infor_tabbar_collection_vis);
            this.mCollectView.setTag(true);
        } else {
            this.mCollectView.setImageResource(R.drawable.detail_collect_black_selector);
            this.mCollectView.setTag(false);
        }
    }


    /************************ 操作数据的方法 end ******************/

    /********************** 所有事件 start **********************/

    /**
     * 单机事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {

        if (v == null) {
            return;
        }
        switch (v.getId()) {
            case R.id.game_right://分享的按钮
                share1();
                StatService.onEvent(GameRaidersVideoActivity.this, "030", "新闻右上角更多", 1);// 统计的点击
                break;
            case R.id.error_page://重新加载页面数据
                loadData();
                break;
            case R.id.newsDetail_comment_layout://点击评论总数
                swithPage();
                StatService.onEvent(GameRaidersVideoActivity.this, "039", "新闻底部评论总数按钮", 1);// 统计的点击
                break;
            case R.id.load_empty_page://空评论点击
                showCommentDialog();
                break;
            case R.id.tv_comment://展示评论弹窗
                showCommentDialog();
                StatService.onEvent(GameRaidersVideoActivity.this, "038", "新闻底部评论输入框", 1);// 统计的点击
                break;
            case R.id.iv_collect://文章收藏
                save();
                StatService.onEvent(GameRaidersVideoActivity.this, "037", "新闻底部收藏按钮", 1);// 统计的点击
                break;
//            case R.id.page_head_function://右上角更多按钮
//                share1();
//                StatService.onEvent(GameRaidersVideoActivity.this, "030", "新闻右上角更多", 1);// 统计的点击
//                break;
            case R.id.iv_share://文章分享
                share();
                StatService.onEvent(GameRaidersVideoActivity.this, "030", "新闻下方分享按钮", 1);// 统计的点击
                break;
            case R.id.iv_report://举报
                reportNews();
                break;
            case R.id.game_download://去玩按钮
                openGame();
                break;

        }

    }

    /**
     * 打开游戏
     */
    private void openGame() {

        if (mNewsContent == null) {
            return;
        }
        if (mNewsContent.isinstall()) {
            //立即打开
            AppTools.startUpApp(this, mNewsContent.getPackageName());
            DownloadUtils.getSingleton().requestStatisticsInterface(this.mNewsContent.getGameId(), DownloadUtils.CLICKTYPE_STARTUP);
        } else {
            //跳转内页
            Intent intent = new Intent(this, GameDetailActivity.class);
            intent.putExtra(Config.Extra.PARAMETER1, gameid);
            startActivity(intent);
        }
    }


    /**
     * 举报
     */
    private void reportNews() {
        if (reportDialog == null) {
            reportDialog = new ReportDialog(mActivity);
        }
        if (mNewsContent != null) {
            reportDialog.show(mNewsContent.getArticleId(), TypeConstant.PAGE_NEWS, TypeConstant.JUBAO_NEWS);
        }
    }


    /**
     * 分享
     */
    private void share() {
        if (mShareUtils == null) {
            mShareUtils = new AppShareUtils(this);
        }
        mShareUtils.showShareDialog(this.mNewsContent, TypeConstant.PAGE_NEWS_DETAIL);
    }

    /**
     * 分享
     */
    private void share1() {
        if (mShareUtils1 == null) {
            SlideSelectView.onSelectListener selectListener = new SlideSelectView.onSelectListener() {
                @Override
                public void onSelect(int textSize) {
                    UserConfigTools.textSize = textSize;
                    AppConfigManager.getInstance().writeConfig(ConfigKeyConstant.option_textSize, UserConfigTools.textSize);
                    //初始化handler
                    if (mNewsContent == null && detailNewsView != null && detailNewsView.webview != null) {
                        return;
                    }
                    detailNewsView.webview.loadDataWithBaseURL(null, mNewsContent.getArticleContent(), "text/html", "utf-8", null);// 网页刷新
                }
            };
            mShareUtils1 = new AppShareUtils(this, selectListener);
        }
        mShareUtils1.showShareDialog(this.mNewsContent, TypeConstant.PAGE_NEWS);

    }


    /**
     * 收藏
     */
    private void save() {
        if (this.mCollectView == null) {
            ToastUtils.makeText(R.drawable.handle_fail, "操作失败");
            return;
        }
        Object isSave = this.mCollectView.getTag();//获取是否收藏标识
        if (isSave == null || !((boolean) isSave)) {
            //如果没有收藏 则 进行收藏操作
            boolean b = CollectUtils.getInstance().saveNews(newsId);
            if (b) {
                ToastUtils.makeText(R.drawable.infor_pop_collection_yes, "收藏成功");
                this.mCollectView.setImageResource(R.drawable.infor_tabbar_collection_vis);
                this.mCollectView.setTag(true);
            } else {
                ToastUtils.makeText(R.drawable.handle_fail, "操作失败");
            }
        } else {
            //如果已收藏 则 进行取消操作
            boolean b = CollectUtils.getInstance().deleteSaveNews(newsId);
            if (b) {
                ToastUtils.makeText(R.drawable.infor_pop_collection_cancel, "取消收藏");
                this.mCollectView.setImageResource(R.drawable.detail_collect_black_selector);
                this.mCollectView.setTag(false);
            } else {
                ToastUtils.makeText(R.drawable.handle_fail, "操作失败");
            }
        }

    }


    /* 展示评论框 */
    private void showCommentDialog() {
        if (mNewsContent == null || TextUtils.equals(mNewsContent.getArticleType(), TypeConstant.NEWS_SHOP)) {
            //如果是电商文章不让评论
            return;
        }
        if (mCommentListener == null) {
            mCommentListener = new CommentListener() {
                @Override
                public void commentStart() {

                }

                @Override
                public void commentFail(VolleyError volley) {

                }

                @Override
                public void commentSuccess(NewsComment newsComment) {
                    //更新页面
                    if (mCommentListView != null) {
                        mCommentListView.addComment(newsComment);
                    }
                    if (mCommentSum != null) {
                        int num = StringUtils.toInt(mCommentSum.getText().toString(), 0);
                        mCommentSum.setText("" + (++num));
                        if (mCommentSum.getVisibility() == View.GONE) {
                            mCommentSum.setVisibility(View.VISIBLE);
                        }
                    }
                }
            };
        }
        CommentUtils.getInstance().showDialog(this, mPageName, newsId, null, mCommentListener);
    }


    /**
     * 切换页面
     */
    private void swithPage() {

        if (mScrollView == null || detailNewsView == null || mNewsContent == null || !onPageFinish) {
            return;
        }
        if (TextUtils.equals(mNewsContent.getArticleType(), TypeConstant.NEWS_SHOP)) {
            //如果是电商文章则不让跳转评论
            return;
        }
        if (mScrollView.currentPositon == 0) {
            mScrollView.scrollToSecondView();
        } else {
            mScrollView.scrollToFirstView();
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        try {
            if (data != null) {
                UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (requestCode == Config.ResultCode.REQUESTCODE_1
                && resultCode == RESULT_OK && mCommentListView != null && data != null) {
            //改变点赞数
            mCommentListView.notifyZanData(data.getBooleanExtra(Config.Extra.PARAMETER1, false));
        }


    }

    @Override
    protected void onDestroy() {
        CommentUtils.getInstance().dismissDialog();
        if (this.detailNewsView != null) {
            //销毁webview
            this.detailNewsView.destoryWebview();
        }
        super.onDestroy();

    }


    @Override
    protected void onPause() {
        super.onPause();
        JCVideoPlayer.releaseAllVideos();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getRepeatCount() == 0) {
            if (detailNewsView != null && detailNewsView.webview != null
                    && detailNewsView.webview.canGoBack()) {
                detailNewsView.webview.goBack();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBackPressed() {

        if (ActivityUtils.isActivityExistsInStack(MainActivity.class)) {
            super.onBackPressed();
        } else {
            Intent intent = new Intent(this, MainActivity.class);
            ActivityUtils.finishActivity(this);
            startActivity(intent);
        }
    }

}
